#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests


class TiebaSpider():

    def __init__(self, kw, max_pn):
        self.max_pn = max_pn
        self.kw = kw
        self.base_url = "https://tieba.baidu.com/f?kw={}&ie=utf-8&pn={}"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) "
                          "Chrome/70.0.3538.110 Safari/537.36 "
        }
        pass

    def get_url_list(self):
        url_list = []

        for pn in range(0, self.max_pn, 50):
            url = self.base_url.format(self.kw, pn)
            url_list.append(url)
        return url_list

    def get_content(self, url):
        print('url', url)
        response = requests.get(url=url, headers=self.headers)
        return response.content

    def get_items(self, content, idx):
        print(idx)
        with open('08-{}.html'.format(idx), 'wb') as f:
            f.write(content)
        return None

    def save_items(self, items):
        '''
        保存数据
        :param items:
        :return:
        '''
        print(items)
        pass

    def run(self):
        url_list = self.get_url_list()

        for url in url_list:
            content = self.get_content(url)
            items = self.get_items(content, url_list.index(url) + 1)

            self.save_items(items)
        pass


if __name__ == '__main__':
    spider = TiebaSpider("英雄联盟", 150)
    spider.run()
